package com.study.zzedu.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.study.zzedu.R;
import com.study.zzedu.entity.ExercisesItem;
import com.study.zzedu.fragment.tab.onlinetest.OnLineTestFragment;
import com.study.zzedu.ui.exercise.activity.IntroduceItemActivity;
import com.study.zzedu.utils.AppConstant;

import java.util.List;

/**
 * Created by kiros on 2020/9/6.
 * 在线测试adapter
 */
public class ExercisesAdapter extends RecyclerView.Adapter {

    private OnLineTestFragment fragment;
    private List<ExercisesItem> exercisesItems;
    private final static int HEAD_COUNT = 1;

    private final static int TYPE_HEAD = 0;
    private final static int TYPE_CONTENT = 1;

    public ExercisesAdapter(OnLineTestFragment fragment, List<ExercisesItem> exercisesItem) {
        this.fragment = fragment;
        this.exercisesItems = exercisesItem;
    }

    @Override
    public int getItemViewType(int position) {
        return TYPE_CONTENT;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(fragment.getContext()).inflate(R.layout.layout_exercises, parent, false);
        return new ContentHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
         if (holder instanceof ContentHolder) { // 内容
            final ExercisesItem item = exercisesItems.get(position);

            ContentHolder mHolder = (ContentHolder) holder;
            mHolder.name.setText(item.getName());
            mHolder.score.setText(item.getCurScore() + "/" + item.getTotalScore());
            mHolder.tips.setText("快来练习吧，每天进步一点点");
            mHolder.status.setVisibility(View.VISIBLE);
            if (item.getHasTestTimes() > 0) {
                mHolder.status.setVisibility(View.INVISIBLE);
                mHolder.go.setText("再做一遍");
                mHolder.go.setTextColor(Color.WHITE);
                mHolder.go.setBackground(fragment.getContext().getDrawable(R.drawable.button_edge3));
            }
             mHolder.go.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View view) {
                     AppConstant.chapterCode = item.getChapterCode();
                     AppConstant.chapterName = item.getName();
                     Intent intent = new Intent(fragment.getContext(), IntroduceItemActivity.class);
                     fragment.startActivityForResult(intent,AppConstant.RequestCode_OnLineToErrorSet);
                 }
             });
        } else { // 尾部

        }
    }

    @Override
    public int getItemCount() {
        return exercisesItems.size();
    }

    // 头部
    private class HeadHolder extends RecyclerView.ViewHolder {
        public HeadHolder(View itemView) {
            super(itemView);
        }
    }

    public class ContentHolder extends RecyclerView.ViewHolder {

        //章节编号
        //private TextView chapterCode;
        //已测试次数
        //private TextView hasTestTimes;
        //当前得分 章节名称
        private TextView score, name, tips, status, go;
        //总分
        //private TextView totalScore;

        public ContentHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.tv_test_chapter_name);
            tips = itemView.findViewById(R.id.tv_test_tip);
            status = itemView.findViewById(R.id.tv_test_status);
            score = itemView.findViewById(R.id.tv_test_score);
            go = itemView.findViewById(R.id.tv_test_go);
        }
    }
}